package com.miyake.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.miyake.client.profile.UserElement;
import com.miyake.client.profile.UserProperty;
import com.miyake.client.tools.ImageListElement;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class DsUser {
	@PrimaryKey 
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)  
	private Key key;
	
	@Persistent
	private String email;

	@Persistent
	private String id;	
	
	@Persistent(mappedBy = "parent") 
	@Element(dependent = "true")
	private List<DsUserElement> properties = new ArrayList<DsUserElement>();
	
	@Persistent
	private List<String> friends = new ArrayList<String>();

	@Persistent
	private List<String> communities = new ArrayList<String>();
	
	@Persistent
	private List<String> images = new ArrayList<String>();
	
	@Persistent
	private Integer imageIndex = new Integer(0);
	
	@Persistent
	private List<String> analysisPluginKeys = new ArrayList<String>();

	@Persistent
	private List<String> filePluginKeys = new ArrayList<String>();

	@Persistent
	private List<String> chartPluginKeys = new ArrayList<String>();
	
	public String getEncodedKey() {
		return KeyFactory.keyToString(key);
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<DsUserElement> getProperties() {
		return properties;
	}

	public void setProperties(List<DsUserElement> properties) {
		this.properties = properties;
	}
	
	public void addElement(String name, String value) {
		DsUserElement element = new DsUserElement(name, value);
		this.properties.add(element);
	}
	
	public List<String> getFriends() {
		return friends;
	}

	public void setFriends(List<String> friends) {
		this.friends = friends;
	}

	public List<String> getCommunities() {
		return communities;
	}

	public void setCommunities(List<String> communities) {
		this.communities = communities;
	}

	public UserProperty toRemoteObject() {
		UserProperty ret = new UserProperty();
		ret.setKey(getEncodedKey());
		ret.setEmail(email);
		if (images.size() > 0) {
			ret.setImageURL("fileServlet?key=" + images.get(this.imageIndex));
		}
		for (DsUserElement element : properties) {
			ret.getProperties().add(new UserElement(element.getName(), element.getValue()));
		}
		return ret;
	}

	public ImageListElement toImageListElement() {
		ImageListElement ret = new ImageListElement();
		if (images.size() > 0) {
			ret.setImageURL("fileServlet?key=" + images.get(this.imageIndex));
		}
		ret.setKey(getEncodedKey());
		for (DsUserElement e : properties) {
			e.getName();
		}
		ret.setName(properties.get(0).getValue());
		return ret;
	}
	
	public String getName() {
		return properties.get(0).getValue();
	}

	public String getImage() {
		return images.get(this.imageIndex);
	}

	public void addFriend(String encodedKey) {
		boolean canAdd = true;
		for (String s : friends) {
			if (s.equals(encodedKey)) {
				canAdd = false;
			}
		}
		if (canAdd) {
			friends.add(encodedKey);
		}
	}

	public void removeFriend(String encodedKey) {
		for (String s : friends) {
			if (s.equals(encodedKey)) {
				friends.remove(s);
			}
		}
	}

	public void addImage(String image) {
		this.images.add(image);
	}

	public List<String> getImages() {
		return images;
	}

	public void setImages(List<String> images) {
		this.images = images;
	}

	public Integer getImageIndex() {
		return imageIndex;
	}

	public void setImageIndex(Integer imageIndex) {
		this.imageIndex = imageIndex;
	}

	public List<ImageListElement> toImageList() {
		List<ImageListElement> ret = new ArrayList<ImageListElement>();
		for (String s : this.images) {
			ImageListElement e = new ImageListElement();	
			e.setImageURL("fileServlet?key=" + s);
			e.setKey(this.getEncodedKey());
			e.setName(this.getName());
			ret.add(e);
		}
		return ret;
	}

	public List<String> getAnalysisPluginKeys() {
		return analysisPluginKeys;
	}

	public void setAnalysisPluginKeys(List<String> analysisPluginKeys) {
		this.analysisPluginKeys = analysisPluginKeys;
	}

	public List<String> getFilePluginKeys() {
		return filePluginKeys;
	}

	public void setFilePluginKeys(List<String> filePluginKeys) {
		this.filePluginKeys = filePluginKeys;
	}

	public void setChartPluginKeys(List<String> chartPluginKeys) {
		this.chartPluginKeys = chartPluginKeys;
	}

	public List<String> getChartPluginKeys() {
		return chartPluginKeys;
	}

	
}
